//
//  PGSegmentCtrl.m
//  EpointFrame5
//
//  Created by Pill Gong on 14-4-16.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "PGSegmentCtrl.h"
#import "UIView+Addtions.h"
#import "UIImage+Tint.h"

@interface PGSegmentCtrl ()

@property (nonatomic, strong) UIButton *prevBtn;
@property (nonatomic, strong) UIButton *nextBtn;

@end

@implementation PGSegmentCtrl

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //self.backgroundColor = [UIColor yellowColor];
        [self addButtons];
    }
    return self;
}

- (void)addButtons {
    self.prevBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _prevBtn.backgroundColor = [UIColor clearColor];
    UIImage *prevImage = [[UIImage imageNamed:@"prevMail"] imageWithTintColor:[UIColor whiteColor]];
    [_prevBtn setImage:prevImage forState:UIControlStateNormal];
    //[_prevBtn setImage:[UIImage imageNamed:@"btn_nav_prev_w"] forState:UIControlStateNormal]
    //_prevBtn.backgroundColor = [UIColor redColor];
    _prevBtn.frame = CGRectMake(0, 0, 40, 30);
    _prevBtn.centerY = PGSegmentCtrlHeight*0.5;
    [_prevBtn addTarget:self action:@selector(prevBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_prevBtn];
    
    self.nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _nextBtn.backgroundColor = [UIColor clearColor];
    UIImage *nextImage = [[UIImage imageNamed:@"nextMail"] imageWithTintColor:[UIColor whiteColor]];
    [_nextBtn setImage:nextImage forState:UIControlStateNormal];
    //[_prevBtn setImage:[UIImage imageNamed:@"btn_nav_prev_w"] forState:UIControlStateNormal]
    //_nextBtn.backgroundColor = [UIColor greenColor];
    _nextBtn.frame = CGRectMake(40, 0, 40, 30);
    _nextBtn.centerY = PGSegmentCtrlHeight*0.5;
    [_nextBtn addTarget:self action:@selector(nextBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_nextBtn];
}

- (void)prevBtnPressed:(id)sender {
    _selectedSegmentIndex = 0;
    [self resoponsePressAtcion];
}

- (void)nextBtnPressed:(id)sender {
    _selectedSegmentIndex = 1;
    [self resoponsePressAtcion];
}

- (void)resoponsePressAtcion {
    if (_delegate && [_delegate respondsToSelector:@selector(segmentCtrlDidSelected:)]) {
        [_delegate segmentCtrlDidSelected:self];
    }
}

- (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment {
    if (segment == 0) {
        [_prevBtn setEnabled:enabled];
    } else if (segment == 1) {
        [_nextBtn setEnabled:enabled];
    }
}

@end
